package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import test.RunningTime;

public class TimeOut
{
	// maintains a thread for executing the doWork method
	private ExecutorService executor = Executors.newFixedThreadPool(1);

	public void loadTimeSeries() throws InterruptedException
	{
		System.out.println("Start Loading Riskfactors");
		Thread.sleep(4*1000L);
		System.out.println("End Loading Riskfactors");
	}

	public void doWorkWithTimeout(int timeoutSecs)
	{

		// set the executor thread working
		final Future<?> future = executor.submit(new Runnable()
		{
			public void run()
			{
				try
				{
					loadTimeSeries();
				}
				catch (Exception e)
				{
					throw new RuntimeException(e);
				}
			}
		});

		// check the outcome of the executor thread and limit the time allowed
		// for it to complete
		try
		{
			future.get(timeoutSecs, TimeUnit.SECONDS);
		}
		catch (Exception e)
		{
			// ExecutionException: deliverer threw exception
			// TimeoutException: didn't complete within downloadTimeoutSecs
			// InterruptedException: the executor thread was interrupted

			// interrupts the worker thread if necessary
			future.cancel(true);
			System.out.println("encountered problem while doing some work: " + e );
		}
	}
	

	public static void main(String args[]){
		TimeOut t = new TimeOut();

		RunningTime rt = new RunningTime();
		rt.start();
		System.out.println("Start....");
		t.doWorkWithTimeout(3);
		rt.stop();
		System.out.println("End: "+rt.getElapsedDescr());
	}
}